package com.supermarket.service;

import com.supermarket.entity.DTO.OrderItemDTO;
import com.supermarket.entity.OrderItem;
import com.supermarket.mapper.OrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单商品明细Service实现类
 */
@Service
public class OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 根据订单ID查询订单商品明细
     * @param orderId 订单ID
     * @return 订单商品明细列表
     */
    public List<OrderItemDTO> getOrderItemsByOrderId(Integer orderId) {
        return orderItemMapper.selectOrderItemsByOrderId(orderId);
    }
    
    /**
     * 根据ID查询订单商品明细
     * @param id 明细ID
     * @return 订单商品明细
     */
    public OrderItem getOrderItemById(Integer id) {
        return orderItemMapper.selectOrderItemById(id);
    }
    
    /**
     * 添加单个订单商品明细
     * @param orderItem 订单商品明细
     * @return 是否成功
     */
    public boolean addOrderItem(OrderItem orderItem) {
        return orderItemMapper.insertOrderItem(orderItem) > 0;
    }
    
    /**
     * 批量添加订单商品明细
     * @param orderItems 订单商品明细列表
     * @return 是否成功
     */
    @Transactional
    public boolean addOrderItems(List<OrderItem> orderItems) {
        return orderItemMapper.batchInsertOrderItems(orderItems) > 0;
    }
    
    /**
     * 更新订单商品明细
     * @param orderItem 订单商品明细
     * @return 是否成功
     */
    public boolean updateOrderItem(OrderItem orderItem) {
        return orderItemMapper.updateOrderItem(orderItem) > 0;
    }
    
    /**
     * 删除订单商品明细
     * @param id 明细ID
     * @return 是否成功
     */
    public boolean deleteOrderItem(Integer id) {
        return orderItemMapper.deleteOrderItem(id) > 0;
    }
    
    /**
     * 根据订单ID删除所有商品明细
     * @param orderId 订单ID
     * @return 是否成功
     */
    @Transactional
    public boolean deleteOrderItemsByOrderId(Integer orderId) {
        return orderItemMapper.deleteOrderItemsByOrderId(orderId) > 0;
    }
    
    /**
     * 根据商品ID查询包含该商品的所有订单明细
     * @param commodityId 商品ID
     * @return 订单商品明细列表
     */
    public List<OrderItemDTO> getOrderItemsByCommodityId(Integer commodityId) {
        return orderItemMapper.selectOrderItemsByCommodityId(commodityId);
    }
} 